/*
 *  CEScrollBar.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/controls/CEScrollBar.h"


using namespace cocos2d;

namespace cocosext {

#define CE_SLIDER_GROUP_SHIFT_Y 15

CEScrollBarColor* CEScrollBarColor::node(const cocos2d::ccColor4B& scrollBarBkColor, const cocos2d::ccColor4B& scrollBarColor)
{
	CEScrollBarColor *pRet = new CEScrollBarColor(scrollBarBkColor, scrollBarColor);
	CCAssert(pRet != NULL, NULL);
	if (pRet->init())
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}

bool CEScrollBarColor::init()
{
    if (!CEScrollBar::init() )
        return false;

	return true;
}

/////////////////////////////////////////////////////
// public Cocos2d interface

#define VERTEX_COUNT 4

void CEScrollBarColor::draw(void)
{
	CEScrollBar::draw();

	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);

	CCPoint vertices[VERTEX_COUNT];
	glVertexPointer(2, GL_FLOAT, 0, vertices);

	GLfloat currentColor[VERTEX_COUNT];
	glGetFloatv(GL_CURRENT_COLOR,currentColor);
	GLubyte alpha = _scrollBarBkColor.a + (GLubyte)((float)(255 - _scrollBarBkColor.a)*(float)(255 - _opacity)/255.f);
	glColor4ub(_scrollBarBkColor.r, _scrollBarBkColor.g, _scrollBarBkColor.b, alpha);

	CCSize contentSz = this->getContentSize();
	float scaleFactor = CC_CONTENT_SCALE_FACTOR();
	contentSz.width *= scaleFactor;
	contentSz.height *= scaleFactor;

	// draw bk
	vertices[0] = ccp(0, 0); 
	vertices[1] = ccp(contentSz.width, 0);
	vertices[2] = ccp(contentSz.width, contentSz.height); 
	vertices[3] = ccp(0, contentSz.height);
	glDrawArrays(GL_TRIANGLE_FAN, 0, VERTEX_COUNT);

	//draw bar
	if (_barSize > FLT_EPSILON && _barSize < 1.f)
	{
		alpha = _scrollBarColor.a + (GLubyte)((float)(255 - _scrollBarColor.a)*(float)(255 - _opacity)/255.f);
		glColor4ub(_scrollBarColor.r, _scrollBarColor.g, _scrollBarColor.b, alpha);

		float height = contentSz.height*_barSize;
		float top = contentSz.height - (contentSz.height - height)*_barPosition;

		vertices[0] = ccp(0, top - height); 
		vertices[1] = ccp(contentSz.width, top - height);
		vertices[2] = ccp(contentSz.width, top); 
		vertices[3] = ccp(0, top);
		glDrawArrays(GL_TRIANGLE_FAN, 0, VERTEX_COUNT);			
	}

	glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]);

	glEnable(GL_TEXTURE_2D);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}

void CEScrollBarColor::show() 
{ 
	this->setIsVisible(true); 
	this->stopAllActions();
	this->runAction(CCFadeTo::actionWithDuration(LTT_SCROLL_BAR_FADE_IN_TIME, 255));
}
void CEScrollBarColor::hide() 
{ 
	this->stopAllActions();
	this->runAction(CCSequence::actions(CCFadeTo::actionWithDuration((float)this->getOpacity()*LTT_SCROLL_BAR_FADE_OUT_TIME/(float)255.f, 0),
										CCHide::action(), NULL)); 
}

}; // end namespace cocosext

